package com.molyfun.parents.activity.record.itemview;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.rest.QiniuSDK;
import com.molyfun.parents.rest.bean.RecordResponse;
import com.molyfun.parents.service.MediaUtils;
import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.gavinliu.android.lib.shapedimageview.ShapedImageView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:11/3/16.
 *
 * 此类为容器
 */
public class RecordVideoView extends BaseRecordView {

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.record_cover)
    ShapedImageView mRecordCover;
    @InjectView(R.id.record_user)
    TextView mRecordUser;
    @InjectView(R.id.course_name)
    TextView mCourseName;

    public RecordVideoView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public RecordVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public RecordVideoView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_record_video, this);
        ButterKnife.inject(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.record_cover:
                MediaUtils.startVideoAndStopAudio(context, mRecordBean.getUrl(), "视频");
                break;
        }
    }

    /**
     * init view
     *
     */
    public void initData(RecordResponse.RecordBean recordBean) {

        if(recordBean != null){
            mRecordBean = recordBean;
            mRecordCover.setImageResource(R.mipmap.ic_main_side_header_bg);
            mRecordUser.setText(mRecordBean.getUsername());
            if(!TextUtils.isEmpty(recordBean.getUrl())){
                System.out.println("QiniuSDK.getBigUrl(recordBean.getUrl())--->" + QiniuSDK.getBigUrl(recordBean.getUrl()));
                Picasso.with(context)
                        .load(recordBean.getUrl() + "&"+ QiniuSDK.QINIU_URL_VIDEO_POSTFIX)
                        .placeholder(R.mipmap.ic_main_side_header_bg)
                        .into(mRecordCover);
            }else{
                mRecordCover.setImageResource(R.mipmap.ic_main_side_header_bg);
            }
            mCourseName.setText(recordBean.getCoursename());
            mRecordCover.setOnClickListener(this);
            mRecordCover.setOnLongClickListener(this);
        }
    }

}